﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Windows;
using Silvermoon.Shapes;
using Silvermoon.Classes;

namespace Silvermoon.Controls
{
    internal class DatePickerItem : NameValueItem
    {
        private static Stack<DatePickerItem> items = new Stack<DatePickerItem>();
        private static readonly Margin ItemPadding = new Margin(10, 5, 10, 5);

        public DatePickerItem()
            : base()
        {
            Alignment = Silvermoon.Controls.Alignment.Far;
            Padding = ItemPadding;
            AutoHeight = false;
            ValueFont = SystemFonts.Medium;
            Background = PhoneColors.Transparent;
            Margin = new Margin(4);
            NameColor = PhoneColors.MediumLight;
            Chrome = new BorderShape { BorderSize = 2, Color = PhoneColors.Medium };
        }

        public static void DisposeItems()
        {
            foreach (var item in items)
            {
                item.Dispose();
            }
            items.Clear();
        }

        public static DatePickerItem GetItem()
        {
            if (items.Count > 0)
            {
                return items.Pop();
            }
            else
            {
                return new DatePickerItem();
            }
        }

        protected static void ReleaseItem(DatePickerItem item)
        {
            items.Push(item);
        }

        protected internal override void Release()
        {
            base.Release();
            ReleaseItem(this);
        }



        protected override void OnDataChanged()
        {
            base.OnDataChanged();
            PickerData data = this.DataValue as PickerData;
            data = data ?? PickerData.Empty;
            this.Name = data.Name;
            this.Value = data.Value;
        }
    }
}
